package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

import org.apache.commons.dbutils.handlers.BeanListHandler;

import dao.RoomDao;
import model.RoomModel;
import util.C3p0Util;
import util.DBUtil;

public class RoomDaoImpl implements RoomDao {
	@Override
	public List<RoomModel> selectAll() throws Exception {
		return C3p0Util.qr.query("select * from hotelroom",new BeanListHandler<RoomModel>(RoomModel.class));
	}

	@Override
	public RoomModel queryRoomByRoomType(String roomType) throws Exception {
		RoomModel room=new RoomModel();
		Connection connection=DBUtil.getconnection();
		PreparedStatement ps=connection.prepareStatement("SELECT * FROM hotelroom WHERE roomtype=?");
		ps.setString(1, roomType);
		ResultSet rs = ps.executeQuery();
		while(rs.next()){
			room.setHotelid(rs.getInt(1));
			room.setRoomtype(rs.getString(2));
			room.setRoomtypeqty(rs.getInt(3));
			room.setRoomprice(rs.getInt(4));
			room.setRoomfancility(rs.getString(5));
			room.setRoomphoto(rs.getString(6));
		}
		return room;
	}

	@Override
	public void reduceEmptyRoom(String roomtype,int roomIndex) throws Exception {
		Connection connection=DBUtil.getconnection();
		PreparedStatement ps = connection.prepareStatement("UPDATE hotelroom SET roomtypeqty=roomtypeqty-"+roomIndex+" WHERE roomtype=?");
		ps.setString(1, roomtype);
		ps.executeUpdate();
		ps.close();
		connection.close();
	}

	public List<RoomModel> selectType() throws Exception {
		return C3p0Util.qr.query("select roomtype from hotelroom",new BeanListHandler<RoomModel>(RoomModel.class));
	}

	@Override
	public List<RoomModel> allList() throws Exception {
		return C3p0Util.qr.query("select * from hotelroom",new BeanListHandler<RoomModel>(RoomModel.class));
	}


}
